package com.easthome.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easthome.common.excelUtil.ExcelUtils;
import com.easthome.auth.service.SysDeptService;
import com.easthome.auth.service.SysPostService;
import com.easthome.auth.service.SysUserService;
import com.easthome.common.result.Result;
import com.easthome.model.system.SysUser;
import com.easthome.vo.system.AssginRoleVo;
import com.easthome.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 朱敦宇
 * @time: 2023/11/8 11:24
 */
@RestController
@RequestMapping("/admin/system/sysUser")
@Api(tags = "用户管理")
@CrossOrigin
public class SysUserController {
    @Autowired
    private SysUserService service;
    @Autowired
    private SysDeptService deptService;
    @Autowired
    private SysPostService postService;
    //用户分页查询
    @ApiOperation("用户分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable Long page,
                             @PathVariable Long limit,
                             SysUserQueryVo sysUserQueryVo){

        Page<SysUser> page1=new Page<>(page,limit);
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        String name= sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        if(!StringUtils.isEmpty(name)){
            lambdaQueryWrapper.like(SysUser::getName,name);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            lambdaQueryWrapper.ge(SysUser::getCreateTime,createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            lambdaQueryWrapper.le(SysUser::getCreateTime,createTimeEnd);
        }
        service.page(page1,lambdaQueryWrapper);
        page1.getRecords().stream().forEach(item ->{
            Long deptId = item.getDeptId();
            if(deptId!=null){
                item.setDeptName(deptService.getById(deptId).getName());
            }

            Long postId = item.getPostId();
            if(postId!=null){
                item.setPostName(postService.getById(postId).getName());
            }


                }
        );
       return Result.ok(page1);
    }
    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = service.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser user) {
        service.save(user);
        return Result.ok();
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user) {
        service.updateById(user);
        return Result.ok();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        service.removeById(id);
        return Result.ok();
    }
    @ApiOperation("更新用户状态")
    @PutMapping("updateStatu")
    public Result updateStatu(@RequestBody SysUser sysUser){
        if(sysUser.getStatus()==1){
            sysUser.setStatus(0);
        }else {
            sysUser.setStatus(1);
        }
        service.updateById(sysUser);
        return Result.ok();
    }
    @ApiOperation("查询用户拥有角色和所有角色")
    @GetMapping("toAssign/{id}")
    public Result toAssign(@PathVariable("id")Long id){
        return service.toAssign(id);
    }

    @ApiOperation("更改用户所属角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        return service.doAssign(assginRoleVo);
    }
    @ApiOperation(value = "获取当前用户基本信息")
    @GetMapping("getCurrentUser")
    public Result getCurrentUser() {
        Map<String, Object> map = service.getCurrentUser();
        return Result.ok(map);
    }

    @PostMapping("importUser")
    public Result importUser(MultipartFile file)throws Exception{
        String end = service.importUser(file);


        return Result.ok(end);
    }
}
